package code

import (
	"context"
	"fmt"
	"io"
	"os"

	"github.com/docker/docker/api/types/container"
	"github.com/docker/docker/api/types/image"
)

// 后台运行容器
func ContainerBack() {
	cli := InitClient()

	imageName := "bfirsh/reticulate-splines"
	out, err := cli.ImagePull(context.Background(), imageName, image.PullOptions{})
	if err != nil {
		panic(err)
	}
	defer out.Close()
	io.Copy(os.Stdout, out)

	// 创建容器
	resp, err := cli.ContainerCreate(context.Background(), &container.Config{
		Image: imageName,
	}, nil, nil, nil, "c-back")
	if err != nil {
		panic(err)
	}

	// 启动容器
	if err := cli.ContainerStart(context.Background(), resp.ID, container.StartOptions{}); err != nil {
		panic(err)
	}

	fmt.Println(resp.ID)
}
